package com.landon.id.result;


import com.landon.id.model.enums.BaseResultCodeEnum;
import com.landon.id.model.enums.IResultCode;
import lombok.Getter;
import lombok.Setter;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.apache.skywalking.apm.toolkit.trace.TraceContext;

/**
 * @author landon_shi
 * @date 2022/9/27
 */
@Setter
@Getter
@SuppressWarnings("all")
public class ResultWrapper<T> {

    /**
     * 状态码
     */
    private String code;

    /**
     * 内容
     */
    private String message;

    /**
     * 链路id
     */
    private String traceId;

    /**
     * 返回数据内容
     */
    private T data;

    public ResultWrapper() {
        this(BaseResultCodeEnum.SUCCESS.getCode(), BaseResultCodeEnum.SUCCESS.getMessage());
    }

    public ResultWrapper(String code, String message) {
        this.code = code;
        this.message = message;
        this.setTraceId();
    }

    public ResultWrapper(IResultCode resultCode) {
        this(resultCode.getCode(), resultCode.getMessage());
    }

    public ResultWrapper(IResultCode resultCode, T data) {
        this(resultCode);
        this.data = data;
    }

    public ResultWrapper(String code, String message, T data) {
        this(code, message);
        this.data = data;
    }

    public static <T> ResultWrapper<T> success(T data) {
        ResultWrapper<T> objectDataResult = new ResultWrapper<>();
        objectDataResult.setData(data);
        return objectDataResult;
    }

    public static <T> ResultWrapper<T> success() {
        ResultWrapper<T> objectDataResult = new ResultWrapper<>();
        objectDataResult.setData(null);
        return objectDataResult;
    }

    public static <T> ResultWrapper<T> fail(IResultCode resultCode, T t) {
        return new ResultWrapper<>(resultCode.getCode(), resultCode.getMessage(), t);
    }

    public static <T> ResultWrapper<T> fail(String code, String message, T t) {
        return new ResultWrapper<>(code, message, t);
    }

    public static <T> ResultWrapper<T> fail(IResultCode resultCode) {
        return new ResultWrapper<>(resultCode.getCode(), resultCode.getMessage(), null);
    }

    public static <T> ResultWrapper<T> fail(String code, String message) {
        return new ResultWrapper<>(code, message, null);
    }

    @Trace
    public void setTraceId() {
        this.traceId = TraceContext.traceId();
    }

}
